package com.vito.dcs.water.action;

import com.alibaba.dubbo.config.annotation.Reference;
import com.vito.base.authoriza.action.BaseAction;
import com.vito.base.authoriza.cache.SessionMap;
import com.vito.base.authoriza.model.UserModel;
import com.vito.base.model.DataGrid;
import com.vito.base.model.ResMsg;
import com.vito.base.model.Result;
import com.vito.base.tools.I18nResMsg;
import com.vito.base.util.Validata;
import com.vito.dcs.water.device.model.WaterBlenderModel;
import com.vito.dcs.water.device.service.WaterBlenderService;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Map;

/**
 * @program: WaterblenderAction
 * @description: 水稳搅拌机设备设置表请求控制器
 * @author: rongr
 * @create: 2019/04/10
 */
@Controller
@RequestMapping("/dcs/water/blender")
public class WaterblenderAction extends BaseAction {

    private static final Logger log = LoggerFactory.getLogger(WaterblenderAction.class);

    @Reference
    private WaterBlenderService waterBlenderService;
    
    private static volatile String id = "";

    /**
     * 保存WaterBlenderModel对象
     *
     * @Param: [waterBlenderModel]
     * @return: com.vito.base.model.Result
     */
    @RequestMapping("/saveWaterBlenderModel")
    @ResponseBody
    public Result saveWaterBlenderModel(@RequestParam Map<String, Object> map) {
        Result res = new Result();
        UserModel obj = SessionMap.getObj();
        if (obj == null || Validata.isNullOrEmpty(obj.getUserId())) {
            res.setCode(Result.CODE_LOGIN_REQUIRE);
            res.setMsg(I18nResMsg.MSG_LOGIN_NEED());
            return res;
        }
        
        if(id.equals(map.get("pageTokenId"))){
			res.setCode(Result.CODE_OK);
			return res;
		}
        
        try {
            map.put("updateUser", obj.getUserId());
            
            id = (String) map.get("pageTokenId");
            
            waterBlenderService.saveWaterBlender(map);
            res.setCode(Result.CODE_OK);
            res.setMsg(I18nResMsg.SAVESUC());
        } catch (Exception e) {
            res.setCode(Result.CODE_ERROR);
            res.setMsg(I18nResMsg.SAVEERR());
            log.error(I18nResMsg.SAVEERR(), e);
        }
        return res;
    }

    /**
     * 修改WaterBlenderModel对象
     *
     * @Param: [waterBlenderModel]
     * @return: com.vito.base.model.Result
     */
    @RequestMapping("/updateWaterBlender")
    @ResponseBody
    public Result updateWaterBlender(@RequestParam Map<String, Object> map) {
        Result res = new Result();
        UserModel obj = SessionMap.getObj();
        if (obj == null || Validata.isNullOrEmpty(obj.getUserId())) {
            res.setCode(Result.CODE_LOGIN_REQUIRE);
            res.setMsg(I18nResMsg.MSG_LOGIN_NEED());
            return res;
        }
        
        if(id.equals(map.get("pageTokenId"))){
			res.setCode(Result.CODE_OK);
			return res;
		}
        
        try {
            map.put("updateUser", obj.getUserId());
            id = (String) map.get("pageTokenId");
            waterBlenderService.updateWaterBlender(map);
            res.setCode(Result.CODE_OK);
            res.setMsg(I18nResMsg.SAVESUC());
        } catch (Exception e) {
            res.setCode(Result.CODE_ERROR);
            res.setMsg(I18nResMsg.SAVEERR());
            log.error(I18nResMsg.SAVEERR(), e);
        }
        return res;
    }

    /**
     * 根据stationId分页查询WaterBlenderModel对象
     *
     * @Param: [map]
     * @return: com.vito.base.model.Result
     */
    @RequestMapping("/queryBlenderByPage")
    @ResponseBody
    public Result queryBlenderByPage(@RequestParam Map<String, Object> map) {
        Result res = new Result();
        try {
            DataGrid<Map<String, Object>> queryRes = waterBlenderService.queryBlenderByPage(map);
            res.setCode(Result.CODE_OK);
            res.setData(queryRes);
        } catch (Exception e) {
            res.setCode(Result.CODE_ERROR);
            res.setMsg("查询失败");
            log.error("查询WaterBlender失败", e);
        }
        return res;
    }

    /**
     * 根据Id查询WaterBlenderModel对象
     *
     * @Param: [map]
     * @return: com.vito.base.model.Result
     */
    @RequestMapping("/queryWaterBlenderById")
    @ResponseBody
    public Result queryWaterBlenderById(@RequestParam Map<String, Object> map) {
        Result res = new Result();
        try {
            WaterBlenderModel waterBlenderModel = waterBlenderService.queryWaterBlenderById(map);
            res.setCode(Result.CODE_OK);
            res.setData(waterBlenderModel);
        } catch (Exception e) {
            res.setCode(Result.CODE_ERROR);
            res.setMsg("查询失败");
            log.error("查询WaterBlender失败", e);
        }
        return res;
    }

    /**
     * 删除WaterStationModel对象
     *
     * @Param: [waterStationModel]
     * @return: com.vito.base.model.Result
     */
    @RequestMapping("/delBlenderModel")
    @ResponseBody
    public Result delBlenderModel(@RequestParam Map<String, Object> map) {
        Result res = new Result();
        try {
            waterBlenderService.delBlenderModel(map);
            res.setCode(Result.CODE_OK);
        } catch (Exception e) {
            res.setCode(Result.CODE_ERROR);
            res.setMsg("删除失败");
        }
        return res;
    }
    
    @RequestMapping("updateIsCurrent")
	@ResponseBody
	public Result updateIsCurrent(@RequestParam Map<String,Object> map){
		Result rs = new Result();
		try{
			waterBlenderService.updateIsCurrent(map);
			rs.setCode(Result.CODE_OK);
			rs.setMsg(ResMsg.DELCOM);
		}catch(Exception ex){
			log.error("修改成功",ex);
			rs.setCode(Result.CODE_ERROR);
			rs.setMsg(ResMsg.EXECERR);
		}
		return rs;
	}
    
    @RequestMapping("findBlenderBySectionId")
	@ResponseBody
	public Result findBlenderBySectionId(@RequestParam Map<String,String> map){
		Result rs = new Result();
		try{
			rs.setData(waterBlenderService.findBlenderBySectionId(map));
			rs.setCode(Result.CODE_OK);
			rs.setMsg(ResMsg.DELCOM);
		}catch(Exception ex){
			log.error("修改成功",ex);
			rs.setCode(Result.CODE_ERROR);
			rs.setMsg(ResMsg.EXECERR);
		}
		return rs;
	}

    /**
     * 根据工艺ID 和 搅拌机编号获取是否存在记录
     * @param map
     * @return
     */
    @RequestMapping("queryWaterLayerIdMachineNo")
    @ResponseBody
    public Result queryWaterLayerIdMachineNo(@RequestParam Map<String,String> map){
        Result rs = new Result();
        try{
            rs.setData(waterBlenderService.queryWaterLayerIdMachineNo(map));
            rs.setCode(Result.CODE_OK);
            rs.setMsg(ResMsg.DELCOM);
        }catch(Exception ex){
            log.error("查询失败",ex);
            rs.setCode(Result.CODE_ERROR);
            rs.setMsg(ResMsg.EXECERR);
        }
        return rs;
    }
}

